package com.spring.component.test.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;

public class StudentBean implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware {
    private String name;
    private int age;

    private String beanName;
    private BeanFactory beanFactory;

    public StudentBean() {
        System.out.println("[bean构造方法]学生类的无参构造方法");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("[setter注入] 注入学生的name属性");
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        System.out.println("[setter注入] 注入学生的age属性");
        this.age = age;
    }

    public void myInit() {
        System.out.println("[init-method]调用init-method属性配置的初始化方法");
    }

    public void myDestory() {
        System.out.println("[destroy-method] 调用destroy-method属性配置的销毁方法");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("[InitializingBean] 调用InitializingBean接口的afterPropertiesSet方法");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("[DisposableBean接口] 调用DisposableBean接口的destroy方法");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
        System.out.println("[BeanFactoryAware接口] 调用BeanFactoryAware的setBeanFactory方法得到beanFactory引用");
    }

    @Override
    public void setBeanName(String s) {
        this.name = s;
        System.out.println("[BeanNameAware接口] 调用BeanNameAware的setBeanName方法得到Bean的名称：" + s);
    }

    @Override
    public String toString() {
        return "StudentBean{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", beanName='" + beanName + '\'' +
                '}';
    }
}
